package com.futures.investm.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVUser;
import com.futures.investm.MainActivity;
import com.futures.investm.R;
import com.futures.investm.common.LoginActivity;
import com.futures.investm.common.SplashActivity;
import com.futures.investm.constant.BussConstant;
import com.futures.investm.event.MineEvent;
import com.futures.investm.utils.CacheData;
import com.futures.investm.utils.IntentUtils;
import com.vise.xsnow.event.Subscribe;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import tech.com.commoncore.base.BaseTitleFragment;
import tech.com.commoncore.dialog.ClearDialog;
import tech.com.commoncore.manager.GlideManager;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.StackUtil;

public class MineFragment extends BaseTitleFragment {
    @BindView(R.id.img_user)
    ImageView imgUser;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_user_setting)
    LinearLayout llUserSetting;
    @BindView(R.id.ll_pw_setting)
    LinearLayout llPwSetting;
    @BindView(R.id.ll_msg)
    LinearLayout llMsg;
    @BindView(R.id.ll_cache)
    LinearLayout llCache;
    @BindView(R.id.btn_sign_out)
    Button btnSignOut;
    AVUser mUser;

//    @BindView(R.id.tv_add_num)
//    TextView tv_add_num;
//    @BindView(R.id.tv_msg_num)
//    TextView tv_msg_num;



    public static MineFragment newInstance() {
        Bundle args = new Bundle();
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getContentLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        getUserData();
    }



    private void getUserData() {
        mUser = CacheData.getLoginUser();
        if (mUser != null) {
            GlideManager.loadCircleImg(mUser.get(BussConstant.HEAD_URL), imgUser, R.mipmap.portrait);
            tvName.setText(mUser.getString(BussConstant.NICKNAME));
        }
    }

    @Subscribe
    public void updateData(MineEvent event) {
        getUserData();
    }


    @OnClick({R.id.ll_user_setting, R.id.ll_pw_setting, R.id.ll_msg, R.id.ll_cache, R.id.btn_sign_out,R.id.ll_feedback,R.id.ll_about,R.id.tv_name})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_user_setting:
                IntentUtils.goUser(mContext);
                break;
            case R.id.ll_pw_setting:
                IntentUtils.goChangePassword(mContext);
                break;
            case R.id.ll_msg:
                IntentUtils.goMsg(mContext);
                break;
            case R.id.ll_cache:
                //清除缓存
                ClearDialog carClearDialog = new ClearDialog();
                carClearDialog.setClearClickListener(new ClearDialog.ClearClickListener() {
                    @Override
                    public void onClearClick() {
                        try {
                            File cacheFile = getActivity().getApplicationContext().getCacheDir();
                            deleteFile(cacheFile);
                            Toast.makeText(getActivity(), "缓存已清空", Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "清空缓存失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                carClearDialog.show(getActivity().getSupportFragmentManager(), "ClearDialog");
                break;
            case R.id.btn_sign_out:
                getSignOut();
                break;
            case R.id.ll_feedback:
                IntentUtils.goFeedbackActivity(mContext);
                break;
            case R.id.ll_about:
                IntentUtils.goAboutUsActivity(mContext);
                break;
            case R.id.tv_name:
                if(!CacheData.isLogin(mContext)) {
                    FastUtil.startActivity(mContext, LoginActivity.class);
                }
                break;
        }
    }

    /**
     * 清空指定文件夹文件
     */
    public static void deleteFile(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory() && !file.getName().equals("diskCache")) {
                File files[] = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    deleteFile(files[i]);
                }
            }
            // file.delete();
        }
    }

    //登出
    private void getSignOut() {
        AVUser.logOut();// 清除缓存用户对象tv_name
        //清除登录信息
        CacheData.setLoginUserId(mContext, "");
        CacheData.setLoginUser(null);
        CacheData.setLoginPassword(mContext, "");
        FastUtil.startActivity(mContext, LoginActivity.class);
        StackUtil.getInstance().popAllExceptCurrent(LoginActivity.class);
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setLeftText("我的").setLeftTextDrawable(null)
                .setRightTextDrawable(R.mipmap.setting_w)
                .setOnRightTextClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!CacheData.isLogin(mContext)) {
                    FastUtil.startActivity(mContext, LoginActivity.class);
                }else {
                    IntentUtils.goUser(mContext);
                }

            }
        });
    }
}
